package logic

import (
	"context"
	"errors"

	"GoZero/mall/model/api/internal/models"
	"GoZero/mall/model/api/internal/svc"
	"GoZero/mall/model/api/internal/types"
	"GoZero/mall/user/rpc/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderLogic {
	return &GetOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetOrderLogic) GetOrder(req *types.OrderReq) (resp *types.OrderReply, err error) {
	user, err := l.svcCtx.UserRpc.GetUser(l.ctx, &user.IdRequest{
		Name: req.Name,
		Pwd:  req.Pwd,
	})
	if err != nil {
		return nil, err
	}

	db := l.svcCtx.DB
	var u1 models.Userinfo
	db.Where("name = ?", user.Name).First(&u1)

	if u1.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	return &types.OrderReply{
		Name: user.Name,
		Age:  user.Age,
	}, nil

}
